package com.wwj.code.util.numberUtils;

import java.util.Random;
import java.util.Vector;

public class StringRandomUtils {


    public static void main(String[] args) {
        System.out.println(getRandomString(0, 10));
    }

    /**
     * 获取不重复的随机数据
     *
     * @param maxRange
     * @param needNumber
     * @return
     */
    public static String getRandomString(int maxRange, int needNumber) {
        String result = "";
        //创建一个产生随机数的对象
        Random r = new Random();

        //创建一个存储随机数的集合
        Vector<Integer> v = new Vector<>();

        //定义一个统计变量
        int count = 0;

        while (count < needNumber) {
            int number = r.nextInt(maxRange) + 1;

            //判断number是否在集合中存在
            if (!v.contains(number)) {
                //不在集合中，就添加
                v.add(number);
                count++;
            }
        }

        //遍历输出
        for (int i : v) {

            result = result + i + ",";
//            System.out.println(i);
        }
        result = result.substring(0, result.length() - 1);
        return result;
    }


}
